package com.checkoutcheckins.client.panels;

import java.util.Date;
import java.util.TreeMap;

import com.checkoutcheckins.client.data.Utils;
import com.checkoutcheckins.client.data.foursquare.Checkin;
import com.checkoutcheckins.client.data.foursquare.FoursquareCheckins;
import com.checkoutcheckins.client.data.foursquare.FoursquareUser;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.visualization.client.CommonOptions;
import com.google.gwt.visualization.client.DataTable;
import com.google.gwt.visualization.client.LegendPosition;
import com.google.gwt.visualization.client.VisualizationUtils;
import com.google.gwt.visualization.client.AbstractDataTable.ColumnType;
import com.google.gwt.visualization.client.visualizations.AreaChart;

public class CheckinsPanel extends VerticalPanel {

	public CheckinsPanel(FoursquareUser fsUser, FoursquareCheckins fsCheckins) {
		super();
		init(fsUser, fsCheckins);
	}

	private void init(FoursquareUser fsUser, FoursquareCheckins fsCheckins) {

		this.setStyleName("checkinsPanel");
		
		System.out.println(fsCheckins.getCheckins().size());

		HTML checkinNum = new HTML("<span style='font-size: 200%'>"
				+ fsCheckins.getCheckins().size() + "</span>"
				+ " Total Checkins");
		checkinNum.setStyleName("center");

		this.add(checkinNum);
		drawCheckinChart(fsCheckins, this);
		this.add(new HTML("<span style='font-size: 70%'>Favoirte city:</span> "
				+ Utils.getMostFrequenctCity(fsCheckins)));
		this.add(new HTML("<span style='font-size: 70%'>Favorite venue:</span> "
				+ Utils.getMostFrequentVenue(fsCheckins)));
	}

	private void drawCheckinChart(final FoursquareCheckins fsCheckins,
			final Panel panel) {

		final DateTimeFormat dtf = DateTimeFormat.getFormat("MMM ''yy");

		final TreeMap<Date, Integer> monthMap = new TreeMap<Date, Integer>();

		for (Checkin checkin : fsCheckins.getCheckins()) {

			Date mmyy = dtf.parse(dtf.format(checkin.getCreatedDate()));

			if (monthMap.containsKey(mmyy)) {
				monthMap.put(mmyy, monthMap.get(mmyy) + 1);
			} else {
				monthMap.put(mmyy, 1);
			}
		}

		Runnable onChartLoad = new Runnable() {

			public void run() {
				DataTable data = DataTable.create();
				data.addColumn(ColumnType.STRING, "Date");
				data.addColumn(ColumnType.NUMBER, "Checkins");
				data.addRows(monthMap.size());

				int x = 0;

				for (Date date : monthMap.keySet()) {
					data.setValue(x, 0, dtf.format(date));
					data.setValue(x, 1, monthMap.get(date));
					x++;
				}

				AreaChart.Options ops = AreaChart.Options.create();
				ops.setWidth(290);
				ops.setHeight(100);
				ops.setLegend(LegendPosition.NONE);
				ops.setColors();

				AreaChart chart = new AreaChart(data, ops);
				panel.add(chart);
			}
		};

		VisualizationUtils.loadVisualizationApi(onChartLoad, AreaChart.PACKAGE);

	}

}
